package com.example.androidmainshixun.view;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androidmainshixun.R;
import com.example.androidmainshixun.database.DBHelper;
import com.example.androidmainshixun.entity.User;
import com.example.androidmainshixun.utils.SpUtils;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "tag";
    public static final int REQUEST_CODE_REGISTER = 1;
    private Button btnLogin;
    private EditText etAccount,etPassword;
    private CheckBox cbRemember,cbAutoLogin;
    private SharedPreferences mSharedPreferences;
    private TextView mtoRegister;
    private SpUtils mSpUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
       btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = etAccount.getText().toString();
                String pass = etPassword.getText().toString();
                Log.d(TAG, "onClick: -------------" + username);
                Log.d(TAG, "password: -------------" + pass);
                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(LoginActivity.this, "请输入账号！", Toast.LENGTH_LONG).show();
                    return;
                } else if (TextUtils.isEmpty(pass)) {
                    Toast.makeText(LoginActivity.this, "请输入密码！", Toast.LENGTH_LONG).show();
                    return;
                }
                User user1= DBHelper.getInstance(LoginActivity.this).getUserByName(username);
                if(user1 ==null){
                    Toast.makeText(LoginActivity.this, "用户名不存在，请注册！", Toast.LENGTH_LONG).show();

                }
                User user= DBHelper.getInstance(LoginActivity.this).login(username,pass);
                if (user==null){
                    Toast.makeText(LoginActivity.this, "用户名或密码错误！", Toast.LENGTH_LONG).show();
                }else {
                    mSpUtils.putInt(SpUtils.LOGIN_KEY, user.getUserid());//记录当前登录学生的id
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    LoginActivity.this.finish();
                }
            }
        });
        mtoRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        cbAutoLogin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    cbRemember.setChecked(true);
                }
            }
        });
        cbRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    cbAutoLogin.setChecked(false);
                }
            }
        });
    }


    private void initView() {
        btnLogin = findViewById(R.id.btn_login);
        etAccount = findViewById(R.id.et_account);
        etPassword = findViewById(R.id.et_password);
        cbRemember = findViewById(R.id.cb_remember);
        cbAutoLogin = findViewById(R.id.cb_auto_login);
        mtoRegister=findViewById(R.id.to_register);
        mSharedPreferences = getSharedPreferences("user_data", MODE_PRIVATE);
        mSpUtils = new SpUtils(this);
        int loginId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
        Log.d("", "login id = " + loginId);
    }

    private void initData() {
        SharedPreferences spf = getSharedPreferences("spfRecord", MODE_PRIVATE);
        boolean isRemember = spf.getBoolean("isRemember", false);
        boolean isLogin = spf.getBoolean("isLogin", false);
        String account = spf.getString("account", "");
        String password = spf.getString("password", "");
        if (isLogin) {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            intent.putExtra("account", account);
            startActivity(intent);
            LoginActivity.this.finish();
        }
        if (isRemember) {
            etAccount.setText(account);
            etPassword.setText(password);
            cbRemember.setChecked(true);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_REGISTER && resultCode == RegisterActivity.RESULT_CODE_REGISTER && data != null) {
            Bundle extras = data.getExtras();
            String username = extras.getString("uaername", "");
            String pass = extras.getString("pass", "");
            etAccount.setText(username);
            etPassword.setText(pass);
        }
    }
}
